package com.ruoyi.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.business.domain.dto.TaskDto;
import com.ruoyi.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.BusRepair;
import com.ruoyi.business.service.IBusRepairService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 维修任务
 * 
 * @author ling
 * @date 2024-07-18
 */
@RestController
@RequestMapping("/business/repair")
public class BusRepairController extends BaseController
{
    @Autowired
    private IBusRepairService busRepairService;
    @Value("${export.max:10000}")
    private Integer max;

    /**
     * 查询维修任务列表
     */
    @PreAuthorize("@ss.hasPermi('business:repair:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusRepair busRepair)
    {
        startPage();
        List<BusRepair> list = busRepairService.selectBusRepairList(busRepair);
        return getDataTable(list);
    }

    /**
     * 导出维修任务列表
     */
    @PreAuthorize("@ss.hasPermi('business:repair:export')")
    @Log(title = "维修任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusRepair busRepair)
    {
        List<BusRepair> list = busRepairService.selectBusRepairList(busRepair);
        if (list.size() > max) {
            throw new ServiceException("导出失败！,每次最多导出 " + max + "条");
        }
        ExcelUtil<BusRepair> util = new ExcelUtil<BusRepair>(BusRepair.class);
        util.exportExcel(response, list, "维修任务数据");
    }

    /**
     * 获取维修任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:repair:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(busRepairService.selectBusRepairById(id));
    }

    /**
     * 新增维修任务
     */
    @PreAuthorize("@ss.hasPermi('business:repair:add')")
    @Log(title = "维修任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusRepair busRepair)
    {
        return toAjax(busRepairService.insertBusRepair(busRepair));
    }

    /**
     * 修改维修任务
     */
    @PreAuthorize("@ss.hasPermi('business:repair:edit')")
    @Log(title = "维修任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusRepair busRepair)
    {
        logger.info("数据:"+ busRepair.toString());
        return toAjax(busRepairService.updateBusRepair(busRepair));
    }

    /**
     * 删除维修任务
     */
    @PreAuthorize("@ss.hasPermi('business:repair:remove')")
    @Log(title = "维修任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(busRepairService.deleteBusRepairByIds(ids));
    }


    /**
     * 派遣任务
     *
     * @param taskDto 任务dto
     * @return {@link AjaxResult }
     */
    @PreAuthorize("@ss.hasPermi('business:repair:dispatch')")
    @Log(title = "维修任务", businessType = BusinessType.TASK)
    @PostMapping("/task")
    public AjaxResult dispatchTask(@RequestBody TaskDto taskDto)
    {
        logger.info(taskDto.toString());
        Long[] ids = taskDto.getRepairIds();
        Long taskId = taskDto.getTaskId();
        logger.info("任务id:{},员工id:{}",taskId,ids);
        return success(busRepairService.dispatchTask(ids, taskId));
    }

    /**
     * 获取信箱中的维修任务(员工端)
     *
     * @return {@link AjaxResult }
     *///    获取维修任务
    @Anonymous
    @GetMapping("/getRepairTask")
    public AjaxResult getRepairTask()
    {
        return success(busRepairService.getRepairTask());
    }

    /**
     * 删除员工信箱中的任务编号
     *
     * @return {@link AjaxResult }
     */
    @Anonymous
    @DeleteMapping("/deleteTaskCode/{id}")
    public AjaxResult deleteTaskCode(@PathVariable("id") Long id)
    {

        return success(busRepairService.deleteTaskCode(id));
    }

    /**
     * 获取管理员的消息
     *
     * @return {@link AjaxResult }
     * @author admin
     * @date 2024/10/17
     */
    @PreAuthorize("@ss.hasAnyRoles('manager,admin')")
    @GetMapping("/getMsgByAdmin")
    public AjaxResult getMsgByAdmin(){
        return success(busRepairService.getMsgByAdmin());
    }

    /**
     * 获取维修任务总数和已复核任务总数
     *
     * @return {@link AjaxResult }
     * @author admin
     * @date 2024/10/17
     */
    @GetMapping("/getRepairAndCompleted")
    @Anonymous
    public AjaxResult getRepairAndCompleted()
    {
        return success(busRepairService.getRepairAndCompleted());
    }
}
